package org.abc.fund.repository.funds;

import org.abc.fund.entity.funds.FundHolding;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.time.LocalDate;
import java.util.List;

public interface FundHoldingRepository extends JpaRepository<FundHolding, Long> {

    // 根据基金代码查询全部持仓（最新/某报告期可选扩展）
    List<FundHolding> findByFundCodeOrderByHoldingRatioDesc(String fundCode);

    // 按基金和报告期查询
    List<FundHolding> findByFundCodeAndReportDateOrderByHoldingRatioDesc(String fundCode, String reportDate);

    // 查询某基金全部持仓（按报告期倒序）
    List<FundHolding> findByFundCodeOrderByReportDateLocalDateDesc(String fundCode);

    // 查询某基金某期的持仓
    List<FundHolding> findByFundCodeAndReportDateLocalDate(String fundCode, LocalDate reportDate);


    boolean existsByFundCodeAndReportDateAndStockCode(String fundCode, String reportDate, String stockCode);

    //用于数据清理
    @Modifying
    @Query("DELETE FROM FundHolding h WHERE h.reportDate LIKE CONCAT(:year, '%')")
    int deleteByReportDateContaining(@Param("year") String year);

    //统计指定年份数据量
    long countByReportDateContaining(String year);

}
